//
//  MainViewController.m
//  ModelParser
//
//  Created by JackAndney on 2018/1/2.
//  Copyright © 2018年 Andney. All rights reserved.
//

#import "MainViewController.h"

#import "ModelParser.h"

#import "NSView+MYBgColor.h"



@interface MainViewController () <ModelParserDelegate>

@property (weak) IBOutlet NSTextField *textField; // 类名输入文本框
@property (weak) IBOutlet NSScrollView *inputView; // JSON字符串输入框
@property (weak) IBOutlet NSScrollView *outputView; // 解析结果

@property (weak) IBOutlet NSView *checkboxContainerView; // 复选框容器视图

@property (weak) IBOutlet NSButtonCell *commentCheckbox; // 注释复选框
@property (weak) IBOutlet NSButtonCell *implementationCheckbox; // 实现文件复选框
@property (weak) IBOutlet NSButtonCell *sortCheckbox; // 排序复选框

@end


@implementation MainViewController

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.checkboxContainerView.backgroundColor = [NSColor clearColor];
    [(NSTextView *)self.outputView.documentView setEditable:NO];
}


- (void)viewDidLoad {
    [super viewDidLoad];
}



#pragma mark -

// 开始解析
- (IBAction)beginParse:(NSButtonCell *)sender {
    NSString *className = self.textField.stringValue;
    if (className.length == 0) {
        [self showAlertMessage:@"请输入类名！"];
        return;
    }
    
    NSString *jsonString = [(NSTextView *)self.inputView.documentView textStorage].string;
    if (jsonString.length == 0) {
        [self showAlertMessage:@"请输入JSON字符串！"];
        return;
    }
    
    // 清空原有数据
    NSTextStorage *outputTextStorage = [(NSTextView *)self.outputView.documentView textStorage];
    [outputTextStorage deleteCharactersInRange:NSMakeRange(0, outputTextStorage.string.length)];
    
    
    // 开始解析
    ModelParser *parser = [ModelParser new];
    parser.jsonString = jsonString;
    parser.className = className;
    parser.delegate = self;
    
    parser.addImplementationPart = self.implementationCheckbox.state == 1;;
    parser.addComment = self.commentCheckbox.state == 1;
    parser.sortedByOrderInString  = self.sortCheckbox.state == 1;;
    
    [parser startParse];
}



#pragma mark -

- (void)parser:(ModelParser *)parser didCompleteWithResult:(NSString *)result {
//    NSLog(@"解析成功result: %@", result);
    
    
    NSTextStorage *outputTextStorage = [(NSTextView *)self.outputView.documentView textStorage];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:result attributes:@{NSFontAttributeName : [NSFont fontWithName:@"Helvetica" size:12]}];
    [outputTextStorage appendAttributedString:attributedString];
}

- (void)parser:(ModelParser *)parser didFailWithError:(NSString *)error {
//    NSLog(@"解析失败: %@", error);
    [self showAlertMessage:error];
}


#pragma mark -

// 消息弹窗
- (void)showAlertMessage:(NSString *)msg {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"确定"];
    [alert setInformativeText:msg];
    alert.messageText = @"温馨提示";
    alert.alertStyle = NSInformationalAlertStyle;
    NSUInteger action = [alert runModal];
    
    if (action == NSAlertFirstButtonReturn) {
        
    }
}


@end
